#include "types.h"
#include "defs.h"
#include "param.h"
#include "stat.h"
#include "mmu.h"
#include "proc.h"
#include "fs.h"
#include "file.h"
#include "fcntl.h"

/*
char*
sput_i(int integer, char *string)
{
	if (integer / 10 != 0) {
		string = sput_i(integer / 10, string);
	}
	*string++ = (char)('0' + integer % 10); 
	return string;
}

char*
sput_ip1(int integer, char *string)
{
	int digit;

	digit = (integer % 10 + 1) % 10;
	if (integer / 10 != 0) {
		string = (digit == 0 ? sput_ip1 : sput_i)(integer / 10, string);
		*string++ = (char)('0' + digit);
	} else {
		if (digit == 0) {
			*string++ = '1';
		}
		*string++ = (char)('0' + digit); 
	}
	return string;
}

void 
itoa(int integer, char *string)
{
	if (0 > integer) {
		++integer;
		*string++ = '-';
		*sput_ip1(-integer, string) = '\0';
	} else {
		*sput_i(integer, string) = '\0';
	}
}*/

 /* reverse:  reverse string s in place */
 void reverse(char s[])
 {
     int i, j;
     char c;
 
     for (i = 0, j = strlen(s)-1; i<j; i++, j--) {
         c = s[i];
         s[i] = s[j];
         s[j] = c;
     }
 }


 void itoa(int n, char s[])
 {
     int i, sign;
 
     if ((sign = n) < 0)  /* record sign */
         n = -n;          /* make n positive */
     i = 0;
     do {       /* generate digits in reverse order */
         s[i++] = n % 10 + '0';   /* get next digit */
     } while ((n /= 10) > 0);     /* delete it */
     if (sign < 0)
         s[i++] = '-';
     s[i] = '\0';
     reverse(s);
 }
 
char *
strcat(char *dest, const char *src)
{
    int i,j;
    for (i = 0; dest[i] != '\0'; i++);
    for (j = 0; src[j] != '\0'; j++)
        dest[i+j] = src[j];
    dest[i+j] = '\0';
    return dest;
}

// Allocate a file descriptor for the given file.
// Takes over file reference from caller on success.
// static int
// fdalloc(struct file *f)
// {
//   int fd;
// 
//   for(fd = 0; fd < NOFILE; fd++){
//     if(proc->ofile[fd] == 0){
//       proc->ofile[fd] = f;
//       return fd;
//     }
//   }
//   return -1;
// }

// static struct inode*
// create_(char *path, short type, short major, short minor)
// {
//   uint off;
//   struct inode *ip, *dp;
//   char name[DIRSIZ];
// 
//   if((dp = nameiparent(path, name)) == 0)
//     return 0;
//   ilock(dp);
// 
//   if((ip = dirlookup(dp, name, &off)) != 0){
//     iunlockput(dp);
//     ilock(ip);
//     if(type == T_FILE && ip->type == T_FILE)
//       return ip;
//     iunlockput(ip);
//     return 0;
//   }
// 
//   if((ip = ialloc(dp->dev, type)) == 0)
//     panic("create: ialloc");
// 
//   ilock(ip);
//   ip->major = major;
//   ip->minor = minor;
//   ip->nlink = 1;
//   iupdate(ip);
// 
//   if(type == T_DIR){  // Create . and .. entries.
//     dp->nlink++;  // for ".."
//     iupdate(dp);
//     // No ip->nlink++ for ".": avoid cyclic ref count.
//     if(dirlink(ip, ".", ip->inum) < 0 || dirlink(ip, "..", dp->inum) < 0)
//       panic("create dots");
//   }
// 
//   if(dirlink(dp, name, ip->inum) < 0)
//     panic("create: dirlink");
// 
//   iunlockput(dp);
//   return ip;
// }
/*

struct file*
kopen(char* path, int omode)
{
  int fd;
  struct file *f;
  struct inode *ip;
  if(omode & O_CREATE){
    begin_trans();
    ip = create_(path, T_FILE, 0, 0);
   commit_trans();
    if(ip == 0)
      return 0;
  } else {
    if((ip = namei(path)) == 0)
      return 0;
    ilock(ip);
    if(ip->type == T_DIR && omode != O_RDONLY){
      iunlockput(ip);
      return 0;
    }
  }
  if((f = filealloc()) == 0 || (fd = fdalloc(f)) < 0){
    if(f)
      fileclose(f);
    iunlockput(ip);
    return 0;
  }
  iunlock(ip);

  f->type = FD_INODE;
  f->ip = ip;
  f->off = 0;
  f->readable = !(omode & O_WRONLY);
  f->writable = (omode & O_WRONLY) || (omode & O_RDWR);

  return f;
}*/
